package com.panda.topic.service.impl;

import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import com.panda.topic.dao.TopicAcceMapper;
import com.panda.topic.dao.TopicMapper;
import com.panda.topic.entity.StudentTopic;
import com.panda.topic.entity.Topic;
import com.panda.topic.entity.TopicAcce;
import com.panda.topic.entity.TopicAcceVo;
import com.panda.topic.service.TopicService;
import com.panda.utils.Pager;
 
@Service
public class TopicServiceImp implements TopicService {
	@Resource
	private TopicMapper topicMapper;
	
	@Resource
	private TopicAcceMapper topicAcceMapper;

	@Override
	public Pager<Topic> findListLimit(Long offSet, Long pageSize, Long uid, String name) {
		List<Topic> topics=topicMapper.findListLimit(offSet, pageSize,uid,name);
		Long count=topicMapper.findListCount(uid,name);
		Pager<Topic> pager=new Pager<Topic>();
		pager.setItems(topics);
		pager.setRowsCount(count);
		return pager;
	}

	@Override
	public Topic findById(Long id) {
		return topicMapper.selectByPrimaryKey(id);
	}

	@Override
	public Long saveOrUpdate(Topic topic) {
		if(topic.getId()!=null){
			Topic oldTopic = topicMapper.selectByPrimaryKey(topic.getId());
			oldTopic.setName(topic.getName());
			oldTopic.setDescription(topic.getDescription());
			oldTopic.setFinishDate(topic.getFinishDate());
			topicMapper.updateByPrimaryKey(oldTopic);
			return topic.getId();
		}else{
			topic.setIsHide(1);
			int id = topicMapper.insert(topic);
			return (long) id;
		}
		
	}

	@Override
	public void delete(Long id) {
		topicMapper.deleteByPrimaryKey(id);
	}

	@Override
	public void addAcce(Long tid, Long fid) {
		TopicAcce topicAcce=new TopicAcce();
		topicAcce.setFid(fid);
		topicAcce.setTid(tid);
		topicAcceMapper.insert(topicAcce);
	}

	@Override
	public List<TopicAcceVo> findAcce(Long tid) {
		return topicAcceMapper.findAll(tid);
	}

	@Override
	public void delAcce(Long taid) {
		topicAcceMapper.deleteByPrimaryKey(taid);
	}

	@Override
	public List<Topic> findAllTopic() {
		return topicMapper.findAllTopic();
	}

	@Override
	public Pager<StudentTopic> findStudentTopic(Long offSet, Long pageSize, Long uid, String name) {
		List<StudentTopic> topics=topicMapper.findStudentTopic(offSet, pageSize,uid,name);
		Long count=topicMapper.findStudentTopicCount(uid,name);
		Pager<StudentTopic> pager=new Pager<StudentTopic>();
		pager.setItems(topics);
		pager.setRowsCount(count);
		return pager;
	}
}
